package au.com.iglootech.component;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.vaadin.terminal.StreamResource.StreamSource;

/**
 * 
 * @author zhuhome
 * 
 */
public class IGTPdf implements StreamSource {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8695848841525933471L;
	
	/**
	 * page size.
	 */
	private static final int SIZE = 50;
	
	/**
	 * output stream.
	 */
	private final ByteArrayOutputStream os = new ByteArrayOutputStream();

	/**
     * 
     */
	public IGTPdf() {
		Document document = null;

		try {
			document = new Document(PageSize.A4, SIZE, SIZE, SIZE, SIZE);
			PdfWriter.getInstance(document, os);
			document.open();

			document.add(new Paragraph(
					"This is some content for the sample PDF!"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (document != null) {
				document.close();
			}
		}
	}

	@Override
	public final InputStream getStream() {
		// Here we return the pdf contents as a byte-array
		return new ByteArrayInputStream(os.toByteArray());
	}
}